package com.pagoda.nerp.web.repositories;

import com.pagoda.nerp.web.entities.HRZGZD;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.query.Param;
import org.springframework.util.CollectionUtils;

import javax.persistence.criteria.*;

import java.util.Set;


/**
 * @Author:HXB
 * @Description:
 * @Date create in 15:35 2018/1/31
 * @Modified By:
 */
public class HRZGZDDaoSpec {
    public static Specification<HRZGZD> getSpec(final Set<String> depts,
                                                final Set<String> codes,
                                                final String mname,
                                                final String code,
                                                final String mobile) {
        return new Specification<HRZGZD>(){
            @Override
            public Predicate toPredicate(Root<HRZGZD> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();

                predicate.getExpressions().add(
                        cb.notEqual(root.get("code"),"updatetime")
                );
                if(!CollectionUtils.isEmpty(depts)) {
                    CriteriaBuilder.In<String> in = cb.in(root.<String> get("dept"));
                    for (String dept : depts) {
                        in.value(dept);
                    }
                    predicate.getExpressions().add(in);
                }
                if(!CollectionUtils.isEmpty(codes)) {
                    CriteriaBuilder.In<String> in = cb.in(root.<String> get("postcode"));
                    for (String code : codes) {
                        in.value(code);
                    }
                    predicate.getExpressions().add(in);
                }
                if(null != mname && !mname.equals("")){
                    predicate.getExpressions().add(
                            cb.like(root.<String>get("name"),mname)
                    );
                }
                if(null !=code && !code.equals("")){
                    predicate.getExpressions().add(
                            cb.like(root.<String>get("code"),code)
                    );
                }
                if(null !=mobile && !mobile.equals("")){
                    predicate.getExpressions().add(
                            cb.like(root.<String>get("mobile"),mobile)
                    );
                }
                return predicate;
            }
        };
    }
}
